package com.zhang.controller;

import com.zhang.pojo.UserBean;
import com.zhang.service.IUserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController  {
    @Autowired
    private IUserService userService;

    @RequestMapping("findByNameAndPwd")
    public String findByNameAndPwd(String username, String pwd, HttpServletRequest req){
        UserBean userBean = userService.findByNameAndPwd(username, pwd);
        if ( userBean != null){
            req.getSession().setAttribute("userBean",userBean);
            return "ok";
        }else {
            return "no";
        }
    }

    @RequestMapping("checkName")
    public String checkName(String username){
        UserBean userBean = userService.findByName(username);
        if (userBean != null){
            return "ok";//等于ok代表用户名已经存在
        } else {
            return "no";
        }
    }

    @RequestMapping("add")
    public String add(UserBean userBean){
        userService.addUser(userBean);
        return "ok";
    }

    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest req){
        UserBean userBean = (UserBean) req.getSession().getAttribute("userBean");
        return userBean;
    }

}
